
<?php
require(dirname(__FILE__) . '/includes/init1.php');

set_time_limit(0);
if($_REQUEST['start_url']){
	$starturl = "http://www.360buy.com/products/1320-1585-1599-0-0-0-0-0-0-0-1-1-1.html";
}
$starturl = "http://www.360buy.com/products/1320-1585-1599-0-0-0-0-0-0-0-1-1-{1,3}.html";
$urllists = array();
if(preg_match("~\{(\d+),(\d+)\}~",$starturl,$pagenum)){ 
	$pagebegin = intval($pagenum[1]); 
	$pageend = intval($pagenum[2]);
	for(;$pagebegin<=$pageend;$pagebegin++){ 
		$url = str_replace($pagenum[0],$pagebegin,$starturl);
		array_push($urllists,$url); 
		
	} 
}
$i = 1;
$time = time();
foreach($urllists as $url){
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL,$url); 
curl_setopt($curl, CURLOPT_REFERER, 'http://www.360buy.com'); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
$result = curl_exec($curl); 
curl_close($curl); 
$sn_list = explode(',',get_str($result,'"skuids" : "','","key":'));
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'timeout'=>30,
   )
);
$context = stream_context_create($opts);

foreach($sn_list as $sn){
	$url = "http://club.360buy.com/clubservice/productcomment-".$sn."-0-0.html";
	$url1 = "http://chat1.360buy.com/api/check.action?pid=".$sn;
	
	$cnt = 0;
	$cnt1 = 0;
	while($cnt < 3 && ($str=@file_get_contents($url,false,$context))===FALSE) $cnt++; 
    while($cnt1 < 3 && ($str1=@file_get_contents($url1,false,$context))===FALSE) $cnt1++;
	if($str&$str1){
		//$aaa = '';
		//$aaa = $sn.'  '.get_str($str,'"CommentCount":',',"AverageScore":').'    '.get_str($str,'"GeneralCount":',',"GeneralRate":').'  '.get_str($str,'"PoorCount":',',"PoorRate":').'   ';
		if(!trim($brankname)){
			$provider = '京东商城';
		}else{
			$provider = iconv('GB2312', 'UTF-8', $brankname);
		}
		$aaa .= $provider.'<br>';
		echo $aaa;
		$commentcount = get_str($str,'"CommentCount":',',"AverageScore":');
		$goodcount = get_str($str,'"GoodCount":',',"GoodRate":');
		$generalcount = get_str($str,'"GeneralCount":',',"GeneralRate":');
		$poorcount = get_str($str,'"PoorCount":',',"PoorRate":');
		$brankname = get_str($str1,'brandName=encodeURIComponent(encodeURIComponent(\'','\'));if(!stock){stock');
		if(!trim($brankname)){
			$provider = '京东商城';
		}else{
			$provider = iconv('GB2312', 'UTF-8', $brankname);
		}
		$db->query("insert into ".$ecs->table('jingdong')." (products_sn,commentcount,goodcount,generalcount,poorcount,provider,ranking,add_time) values ('".$sn."','".$commentcount."','".$goodcount."','".$generalcount."','".$poorcount."','".$provider."','".$i."','".$time."')");
	}
	//插入数据
	unset($str);
	unset($str1);
	$i++;
}
}

function get_str($str,$start_str,$end_str){  
 
   $start_pos = strpos($str,$start_str)+strlen($start_str);  
   $end_pos = strpos($str,$end_str);  
   $c_str_l = $end_pos - $start_pos;  
   $contents = substr($str,$start_pos,$c_str_l);  
   return $contents;  
} 
?>